<template>
  <div class="art-pub-container">
    <el-form
      ref="pubRef"
      label-width="100px"
      :model="pubForm"
      :rules="pubRules"
    >
      <el-form-item label="文章标题" prop="title" placeholder="请填写文章标题">
        <el-input maxlength="30" v-model="pubForm.title"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <el-select placeholder="请选择分类" clearable v-model="pubForm.cate_id">
          <el-option
            :label="item.cate_name"
            :value="item.id"
            v-for="item in cateList"
            :key="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <quill-editor v-model="pubForm.content"></quill-editor>
      </el-form-item>
      <el-form-item label="文章封面" prop="cover_img">
        <div class="cover-box">
          <img :src="pubForm.cover_img" alt="" class="cover-img" />
          <el-link type="primary" :underline="false" @click="chooseCover"
            >+ 选择封面</el-link
          >
          <!--选择图片  默认隐藏  当点击上面选择封面时显示 -->
          <input
            type="file"
            ref="inpFile"
            accept="image/*"
            style="display: none"
            @change="chooseImgFn"
          />
        </div>
      </el-form-item>
      <el-form-item prop="state">
        <el-button type="primary" @click="pubArtFn('已发布')"
          >发布文章</el-button
        >
        <el-button type="info" @click="pubArtFn('草稿')">存为草稿</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入仓库数据  渲染到选择下拉列表
import { mapState } from 'vuex'
import defaultCover from '../../../assets/images/cover.jpg'
// 2.对话框中布局   并显示分类和校验
// 3.图片上传
// 4.发布文章-->发布文章按钮+存为草稿
// 5.关闭对话框
// 6.更新数据
import { pubArticleApi } from '../../../api/art'
export default {
  name: '',
  data () {
    return {
      pubForm: {
        title: '',
        cate_id: '',
        content: '',
        state: '',
        cover_img: defaultCover
      },
      pubRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          {
            min: 1,
            max: 30,
            message: '文章标题的长度为1-30个字符',
            trigger: 'blur'
          }
        ],
        cate_id: [
          { required: true, message: '请选择文章分类', trigger: 'blur' }
        ]
      },
      files: {}
    }
  },
  created () {},
  computed: {
    ...mapState('art', ['cateList'])
  },
  methods: {
    // 点击选择封面时   隐藏的input触发点击事件
    chooseCover () {
      this.$refs.inpFile.click()
    },
    chooseImgFn (e) {
      console.log(e) // files,是一个对象  第一个就是图片的地址
      // 1、获取选择的图片资源
      var file = e.target.files[0]
      if (file.length <= 0) return this.$message('请选择图片!')
      const ImgUrl = URL.createObjectURL(file)
      // 2-将图片临时路径存储起来给img
      this.pubForm.cover_img = ImgUrl
      //  3-存储资源文件
      this.files = file
    },
    pubArtFn (str) {
      // 处理state状态字段
      this.pubForm.state = str
      // 预校验
      this.$refs.pubRef.validate(async (valid) => {
        if (!valid) return ''
        // 2. 处理数据格式为formData格式
        const fm = new FormData()
        fm.append('title', this.pubForm.title)
        fm.append('cate_id', this.pubForm.cate_id)
        fm.append('content', this.pubForm.content)
        fm.append('state', this.pubForm.state)
        fm.append('cover_img', this.files)
        const res = await pubArticleApi(fm)
        console.log(res, 'pusArt')
        if (res.data.code === 0) {
          this.$message({
            message: '发布成功!',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发布失败!',
            type: 'error'
          })
        }
        // 5. 子传父通知父组件 关闭对话框
        this.$emit('closePubDia')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.art-pub-container {
  padding: 0 30px 20px 0px;
}

.el-select {
  width: 100%;
}

/deep/ .ql-snow .ql-color-picker .ql-picker-label svg,
/deep/ .ql-snow .ql-icon-picker .ql-picker-label svg {
  position: relative;
  top: -6px;
}

/deep/ .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

/deep/ .ql-editor {
  min-height: 300px;
}

.cover-img {
  object-fit: cover;
  width: 400px;
  height: 280px;
  margin-right: 10px;
}

.cover-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>
